/*
This work is licensed under Creative Commons GNU GPL License
http://creativecommons.org/licenses/GPL/2.0/
and based on code by Russel Lindsay www.weetbixthecat.com
Copyright Kostik Naumov www.piterpen.net
(c) 20070108
*/

Array.prototype._sortPrep = function ( field ) {
	if (!this.maximum) {
		this.maximum = this[0][field];
		var i = this.length;
		while(i--) {
			if (this.maximum < this[i][field]) {this.maximum = this[i][field];}
		}
	}
	if (this.maximum) {
		var fill = new Array();
		var length = this.maximum.toString().length;
		var i = length; var zs = "";
		while (i--) {
			fill.push(zs); zs += "0";
		}
		for (var i=0; i < this.length; i++ )
			this[i][field] = (this[i][field].toString().length < length ? fill[length - this[i][field].toString().length] : "") + this[i][field];
	}
}


Array.prototype._sortIntAsc = function(field) {
	this._sortPrep(field);
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}

Array.prototype._parseInt = function(field) {
	for (var i=0; i < this.length; i++ ) {
		this[i][field] = this[i][field]*1;
	}
}

Array.prototype.sortIntAsc = function(field) {
	this._sortIntAsc(field);
	this._parseInt(field);
}

